package by.bsu.fpmi.bbtp.dao;

import by.bsu.fpmi.bbtp.authentication.User;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class UserDAO extends AbstractDAO<User> {

    @Override
    protected Class<User> getClassOfObject() {
        return User.class;
    }
    
    public User findUserByName(String username) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Restrictions.eq("name", username.toLowerCase()));
        List<User> users = getHibernateTemplate().findByCriteria(criteria);
        if (users.isEmpty()) {
            return null;
        } else {
            return users.get(0);
        }
    }
}
